/**
  author: tangke
 */

package service

import (
	"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
)

func (t *ServiceSetup) SetInfo(name, num string) (string, error) {

	eventID := "eventSetInfo"
	reg, notifier := regitserEvent(t.Client, t.ChaincodeID, eventID)
	defer t.Client.UnregisterChaincodeEvent(reg)

	req := channel.Request{ChaincodeID: t.ChaincodeID, Fcn: "set", Args: [][]byte{[]byte(name), []byte(num), []byte(eventID)}}
	respone, err := t.Client.Execute(req)
	if err != nil {
		return "", err
	}

	err = eventResult(notifier, eventID)
	if err != nil {
		return "", err
	}

	return string(respone.TransactionID), nil
}


func (t *ServiceSetup) GetInfo(name string) (string, error){

	req := channel.Request{ChaincodeID: t.ChaincodeID, Fcn: "get", Args: [][]byte{[]byte(name)}}
	respone, err := t.Client.Query(req)
	if err != nil {
		return "", err
	}

	return string(respone.Payload), nil
}

// GetOrder 查询订单状态
func (t *ServiceSetup) GetOrder(order string) (string, error) {
	req := channel.Request{ChaincodeID: t.ChaincodeID, Fcn: "getlastvalue", Args: [][]byte{[]byte(order)}}
	response, err := t.Client.Query(req)
	if err != nil {
		return "", err
	}

	return string(response.Payload), nil
}

// SetOrder 设置订单
func (t *ServiceSetup) SetOrder(order, status string) (string, error) {
	req := channel.Request{ChaincodeID: t.ChaincodeID, Fcn: "putvalue", Args: [][]byte{[]byte(order), []byte(status)}}
	response, err := t.Client.Execute(req)
	if err != nil {
		return "", err
	}

	return string(response.TransactionID), nil
}

// SetOrderSend 发货
func (t *ServiceSetup) SetOrderSend(order, status string) (string, error) {
	req := channel.Request{ChaincodeID: t.ChaincodeID, Fcn: "putvaluesend", Args: [][]byte{[]byte(order), []byte(status)}}
	response, err := t.Client.Execute(req)
	if err != nil {
		return "", err
	}

	return string(response.TransactionID), nil
}

// SetOrderRecv 确认
func (t *ServiceSetup) SetOrderRecv(order, status string) (string, error) {
	req := channel.Request{ChaincodeID: t.ChaincodeID, Fcn: "putvaluerecv", Args: [][]byte{[]byte(order), []byte(status)}}
	response, err := t.Client.Execute(req)
	if err != nil {
		return "", err
	}

	return string(response.TransactionID), nil
}

// SetOrderRecv1 收货
func (t *ServiceSetup) SetOrderRecv1(order, status string) (string, error) {
	req := channel.Request{ChaincodeID: t.ChaincodeID, Fcn: "putvaluerecv1", Args: [][]byte{[]byte(order), []byte(status)}}
	response, err := t.Client.Execute(req)
	if err != nil {
		return "", err
	}

	return string(response.TransactionID), nil
}


// RequestLoan 申请贷款
func (t *ServiceSetup) RequestLoan(project, time, num string) (string, error) {
	req := channel.Request{ChaincodeID: t.ChaincodeID, Fcn: "requestloan", 
	Args: [][]byte{[]byte(project), []byte(time), []byte(num)}}

	response, err := t.Client.Execute(req)
	if err != nil {
		return "", err
	}

	return string(response.TransactionID), nil
}

//Loan 发放贷款
func (t *ServiceSetup) Loan(project, loanid, loantime, loannum string) (string, error) {
	req := channel.Request{ChaincodeID: t.ChaincodeID, Fcn: "loan", 
	Args: [][]byte{[]byte(project), []byte(loanid), []byte(loantime), []byte(loannum)}}

	response, err := t.Client.Execute(req)
	if err != nil {
		return "", err
	}

	return string(response.TransactionID), nil
}


//SetQuota 设置贷款
func (t *ServiceSetup) SetQuota(project, quota string) (string, error) {
	req := channel.Request{ChaincodeID: t.ChaincodeID, Fcn: "setquota", 
	Args: [][]byte{[]byte(project), []byte(quota)}}

	response, err := t.Client.Execute(req)
	if err != nil {
		return "", err
	}

	return string(response.TransactionID), nil
}

//QueryHistory 查询历史
func (t *ServiceSetup) QueryHistory(project string) (string, error) {
	req := channel.Request{ChaincodeID: t.ChaincodeID, Fcn: "gethistory", 
	Args: [][]byte{[]byte(project)}}

	response, err := t.Client.Execute(req)
	if err != nil {
		return "", err
	}

	return string(response.Payload), nil
}


//Upload 上传文件
func (t *ServiceSetup) Upload(value string) (string, error) {
	req := channel.Request{ChaincodeID: t.ChaincodeID, Fcn: "setpicture", 
	Args: [][]byte{ []byte(value)} }

	response, err := t.Client.Execute(req)
	if err != nil {
		return "", err
	}

	return string(response.TransactionID), nil
}

//Download 下载图片
func (t *ServiceSetup) Download(value string) (string, error) {
	req := channel.Request{ChaincodeID: t.ChaincodeID, Fcn: "getpicture", 
	Args: [][]byte{ []byte(value)} }

	response, err := t.Client.Query(req)
	if err != nil {
		return "", err
	}

	return string(response.Payload), nil
}